package com.kira.scaffoldmvc.Controller;

import com.kira.scaffoldmvc.HeartBeat.SwitchBarrier;
import com.kira.scaffoldmvc.POJO.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

@Slf4j
@RestController
@RequestMapping("my")
public class TestListenerController {

    @Resource
    SwitchBarrier kiraSwitchBarrier1;

    @Resource
    SwitchBarrier kiraSwitchBarrier2;

    @GetMapping("/testBarrier1")
    public Result testBarrier1() throws InterruptedException {
        if(kiraSwitchBarrier1.canCross()){
            log.info("二级缓存预热完毕，接口放行");
            return Result.success("接口放行");
        }else{
            log.info("缓存没有预热完毕,该接口不能执行");
            kiraSwitchBarrier1.cross();//线程尝试通过,若禁止则阻塞,直到缓存加载完毕后线程被唤醒
            return Result.success("接口拦截,缓存预热完毕后放行成功");
        }
    }

    @GetMapping("/testBarrier2")
    public Result testBarrier2() throws InterruptedException {
        if(kiraSwitchBarrier2.canCross()){
            log.info("二级缓存预热完毕，接口放行");
            return Result.success("接口放行");
        }else{
            log.info("缓存没有预热完毕,该接口不能执行");
            kiraSwitchBarrier2.cross();//线程尝试通过,若禁止则阻塞,直到缓存加载完毕后线程被唤醒
            return Result.success("接口拦截");
        }
    }



}
